package com.cjt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjt.domain.Membership;
import com.cjt.domain.Student;
import com.cjt.service.MembershipService;
import com.cjt.service.StudentService;
import com.cjt.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjt
 * @since 2020-10-26
 */
@RestController
@RequestMapping("/membership")
public class MembershipController {
        @Resource
        private MembershipService membershipService;
        @Resource
        private StudentService studentService;
        @RequestMapping("/getAll")
        public Object selectAll() {
                return membershipService.list(null);
        }

        @RequestMapping("/selectMemByPage")
        public Object selectByMemPage( Membership mem,Page<Membership> page) {
                return membershipService.selectByMemPage(page,mem);
        }
        @RequestMapping("/getAllVue")
        public Object getAllVue(Page<Membership> page) {
                return new Result(0, "成功", membershipService.page(page), membershipService.count());
        }

        @RequestMapping("/update")
        public Object update(@RequestBody Membership mem) {
                return membershipService.updateById(mem);
        }

        @RequestMapping("/add")
        public Object add(@RequestBody Membership mem) {
                Membership mSpe = membershipService.getOne(new QueryWrapper<Membership>().eq("mspe", mem.getMspe()));//使用mybatis plus条件构造器queryWrapper，根据 entity 条件，查询一条记录
                if (mSpe != null) {
                        return new Result(0, "添加失败，该系已存在！");//把查到的结果数据，返回给前端ajax的请求
                }
                return membershipService.save(mem);
        }
        @RequestMapping("/delete")
        public Object delete(@RequestBody Membership mem) {
                List<Student> list = studentService.list(new QueryWrapper<Student>().eq("mid", mem.getMid()));//使用mybatis plus条件构造器queryWrapper，根据 entity 条件，查询一条记录
                if (!list.isEmpty()) {
                        return new Result(0, "该学院下有学生，不能删除！");//把查到的结果数据，返回给前端ajax的请求
                }
                return membershipService.removeById(mem.getMid());
        }

}
